<?php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
	protected $_basePath;
	
	public function _initAutoload()
	{
		$autoloader = Zend_Loader_Autoloader::getInstance();
		$autoloader->setFallbackAutoloader(true);
		
        $autoloader = new Zend_Application_Module_Autoloader(array(
            'namespace' => '',
            'basePath'  => dirname(__FILE__),
        ));
        return $autoloader;
	}
	
	protected function _initView()
	{
		$view = new Zend_View();
		$view->setEncoding('UTF-8');
		$view->doctype('HTML5');
		
		$view->basePath = $this->getBasePath();
		$view->skinPath = $this->getSkinDir();
		$view->imagePath = $this->getImagesDir();
		$view->jsPath = $this->getScriptsDir();
		$view->cssPath = $this->getStylesDir();
		
		$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper(
			'ViewRenderer'
		);
		$viewRenderer->setView($view);

		return $view;
	}
	
	public function getSystemVars() {
		return (object) $this->getOption('systemVar');
	}
	
	public function getSystemVar($type) {
		if(isset($this->getSystemVars()->$type)) {
			return $this->getSystemVars()->$type;
		}
		
		throw new Zend_Exception(
			'Há um problema na recuperação da variável do sistema "'.$type.'" '
			.'do arquivo de configuração'
		);
	}
	
	public function getBasePath() {
		if($this->_basePath == null) {
			$this->setBasePath();
		}
		
		return $this->_basePath;
	}
	
	public function setBasePath($basePath = null)
	{
		if(!empty($basePath)) {
			$this->_basePath = $basePath;
		} else {
			$request = new Zend_Controller_Request_Http();
			
			$this->_basePath = $request->getBasePath();
		}
		
		return $this;
	}
	
	public function getSkinDir() {
		return $this->getBasePath().$this->getSystemVar('skin');
	}
	
	public function getImagesDir() {
		return $this->getSkinDir().$this->getSystemVar('images');
	}
	
	public function getScriptsDir() {
		return $this->getSkinDir().$this->getSystemVar('scripts');
	}
	
	public function getStylesDir() {
		return $this->getSkinDir().$this->getSystemVar('styles');
	}
	
	public function getDb() {
		return Zend_Db_Table::getDefaultAdapter();
	}
	
	public function getCacheDir() {
		return $this->getSystemVar('cache');
	}
	
	public function getCmsTableParamsFile() {
		return $this->getSystemVar('cmstableparams');
	}
}

